package com.android.fragmentdemo.defineviews
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.util.Log
import android.view.animation.AccelerateInterpolator
import androidx.appcompat.widget.AppCompatImageView
import com.android.fragmentdemo.R

class ShadowFloatImageView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : AppCompatImageView(context, attributeSet) {

    private val mShadowPaint by lazy {
        Paint().apply {
            isAntiAlias = true
        }
    }

    private val mMatrix by lazy {
        Matrix()
    }

    private val rectF by lazy {
        RectF()
    }

    private var mGradient : LinearGradient? = null

    private var offsetX = 0F
        set(value) {
            field = value
            invalidate()
        }

    private var offsetY = 0F
        set(value) {
            field = value
            invalidate()
        }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        if (mGradient == null) {
            mGradient = LinearGradient(
                0F,
                0F,
                measuredWidth.toFloat(),
                measuredHeight.toFloat(),
                intArrayOf(
                    //起始颜色
                    resources.getColor(R.color.color_00000000),
                    resources.getColor(R.color.color_ffe3e3e3),
                    resources.getColor(R.color.color_00000000),
                    resources.getColor(R.color.color_ffeeeeee),
                    //中间颜色
                    resources.getColor(R.color.color_00000000),
                    resources.getColor(R.color.color_ffeeeeee),
                    //末尾颜色
                    resources.getColor(R.color.color_00000000),
                    resources.getColor(R.color.color_ffeeeeee),
                    resources.getColor(R.color.color_00000000),
                ),
                floatArrayOf(0F, 0.1F, 0.4F, 0.35f, 0.5F, 0.60F, 0.7F, 0.85F, 1F),
                Shader.TileMode.CLAMP
            )
        }

        mShadowPaint.shader = mGradient
        Log.d(TAG,"measuredWidth:${measuredWidth};measuredHeight:${measuredHeight}")
        canvas.apply {
            drawRect(rectF, mShadowPaint)
        }

        mMatrix.setTranslate(offsetX, offsetY)
        mGradient?.setLocalMatrix(mMatrix)
    }

    fun startAnimation() {
        rectF.set(
            0F,
            0F,
            measuredWidth.toFloat(),
            measuredHeight.toFloat()
        )

        val animationX = ObjectAnimator.ofFloat(this, "offsetX", 0F, measuredWidth * 2.0F)
        val animationY = ObjectAnimator.ofFloat(this, "offsetY", 0F, measuredHeight * 2.0F)

        val animatorSet = AnimatorSet()
        animatorSet.playTogether(animationX, animationY)
        animatorSet.duration = 800
        animatorSet.interpolator = AccelerateInterpolator()
        animatorSet.start()
    }

    companion object {
        const val TAG = "ShadowFloatImageView"
    }
}